package com.apexedu.biz.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.biz.action.ServiceCodeAction;
import com.apexedu.biz.entity.TServiceCode;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.sys.SysConstants;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * 服务类别字典维护
 * 
 * @author 陈璐 datetime 2009-6-7
 */
@Service("serviceCodeService")
public class ServiceCodeServiceImpl extends AbstractBaseServiceSupport {

	public List<?> list(Object myaction, Pager pager) throws Exception {
		ServiceCodeAction action = (ServiceCodeAction) myaction;
		TServiceCode bean = action.getTserviceCode();

		List<Object> params = new LinkedList<Object>();

		String hqlColumn = "select new TServiceCode(a.wid, a.zdbm, a.zdmc, a.zdcc, a.sjdm, " +
				"(select c.zdmc from TServiceCode c where c.wid=a.sjdm) as sjdm, a.sfsy, a.zdlb, a.zs, b.lbmc)";
		String hql = " from TServiceCode a,TServiceCodesort b where a.zdlb=b.zdlb";
		if (bean != null) {
			if (StringUtil.isNotBlank(bean.getZdbm())) {
				hql += " and a.zdbm = ?";
				params.add(bean.getZdbm());
			}
			if (StringUtil.isNotBlank(bean.getZdmc())) {
				hql += " and a.zdmc like ?";
				params.add("%" + bean.getZdmc() + "%");
			}
			if (StringUtil.isNotBlank(bean.getZdlb())) {
				hql += " and a.zdlb = ?";
				params.add(bean.getZdlb());
			}
		}
		String chql = "select count(*) " + hql;
		pager.setTotalRows(getBaseDao().count(chql, params.toArray()));

		hql = hqlColumn + hql + " order by a.zdlb,a.zdpx,a.zdbm, length(a.zdbm)";
		List<?> tserviceCode = getBaseDao().findPageByHql(hql, pager, params.toArray());
		return tserviceCode;
	}

	public void openCreate(Object myaction) throws Exception {
		ServiceCodeAction action = (ServiceCodeAction) myaction;
		String zdlb = action.getParameter("zdlb");
		TServiceCode bean = action.getTserviceCode();
		if (null == bean) {
			bean = new TServiceCode();
			bean.setZdlb(zdlb);
		}
		action.setTserviceCode(bean);
	}

	public void load(Object myaction) throws Exception {
		ServiceCodeAction action = (ServiceCodeAction) myaction;
		String wid = action.getWid();
		TServiceCode bean = (TServiceCode) getBaseDao().findById(TServiceCode.class, wid);

		action.setTserviceCode(bean);
	}

	public void saveOrUpdate(Object myaction) throws Exception {
		ServiceCodeAction action = (ServiceCodeAction) myaction;
		TServiceCode bean = action.getTserviceCode();

		if (bean == null) {

			action.addActionError(SysConstants.MESSAGE_SAVE_FAILURE + "[ 被保存的对象为null ]");
		} else if (bean.getWid() == null || bean.getWid().equals("")) {

			bean.setWid(SeqFactory.getNewSequenceAlone());
			getBaseDao().save(bean);
			BusinessLogUtil.log(action.getLoginuser().getUserloginid(), Const.CZDX_T_SERVICE_CODE,
					SysConstants.LOG_ACTION_SAVE, IdLoginAction.getIpAddr(action.getRequest()));
		} else {
			getBaseDao().merge(bean);
			BusinessLogUtil.log(action.getLoginuser().getUserloginid(), Const.CZDX_T_SERVICE_CODE,
					SysConstants.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(action.getRequest()));
		}
		String syxz = action.getParameter("sfxz");// 保存并新增按钮
		if ("1".equals(syxz)) {
			TServiceCode object = new TServiceCode();
			object.setZdlb(bean.getZdlb());
			action.setTserviceCode(object);
			openCreate(myaction);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.imchooser.framework.service.BaseService#remove(java.lang.Object)
	 */
	public boolean remove(Object myaction) throws Exception {
		ServiceCodeAction action = (ServiceCodeAction) myaction;
		String ids = action.getWid();
		boolean deleteSuccess = getBaseDao().deleteAll("TServiceCode", "wid", "=", ids);
		if (deleteSuccess) {
			BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SERVICE_CODE, SysConstants.LOG_ACTION_DEL,
                    IdLoginAction.getIpAddr(action.getRequest()));
		}
		return deleteSuccess;
	}

}
